package it.fuesi.myseries.activities;

import it.fuesi.myseries.R;
import it.fuesi.myseries.api.ApiBrokerService;
import it.fuesi.myseries.api.ApiMethod;
import it.fuesi.myseries.model.Episode;
import it.fuesi.myseries.model.MySeriesConstant;
import it.fuesi.myseries.model.Season;
import it.fuesi.myseries.model.Serie;
import it.fuesi.myseries.model.adapters.SeasonExpandableListAdapter;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.widget.AbsListView.LayoutParams;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.ExpandableListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.image.SmartImageView;

public class MySerieDetailActivity extends Activity implements Observer {

	private Serie currentSerie;
	
	private SeasonExpandableListAdapter seasonAdapter;
	private ExpandableListView seasons;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_myserie_detail);

		final TextView nameText = (TextView) findViewById(R.id.name);
		final TextView ovewViewText = (TextView) findViewById(R.id.overview);
		final SmartImageView imageView = (SmartImageView) findViewById(R.id.banner);
		
		final Intent intent = getIntent();
		currentSerie = (Serie) intent.getSerializableExtra("EXTRA_MESSAGE");

		nameText.setText(currentSerie.getName());
		ovewViewText.setText(currentSerie.getOverview());

		imageView.setImageUrl(currentSerie.getBanner());
		
		
		seasons = (ExpandableListView) findViewById(R.id.seasons);
		
		seasonAdapter = new SeasonExpandableListAdapter(this, new ArrayList<Season>());

        // Set this blank adapter to the list view
		
		seasons.setIndicatorBounds(0, 20);
		
		seasons.setAdapter(seasonAdapter);
		
		seasons.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int index,
					long arg3) {
//				openDetail(episodes.get(index));
			}
		});
		

		getSeasons();
		
	}
	
//	public void openDetail(Episode episode) {
//		Intent intent = new Intent(this, SerieDetailActivity.class);
//		intent.putExtra("EXTRA_MESSAGE", serie);
//		startActivity(intent);
//	}
	
	public void getSeasons(){
		ProgressBar p = new ProgressBar(seasons.getContext());
		p.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT, Gravity.CENTER));
		p.setIndeterminate(true);
		seasons.setEmptyView(p);
		
		Intent intent = ApiBrokerService.getApiTaskIntent(this, ApiMethod.getSeasons);
		
		intent.putExtra(MySeriesConstant.ID.toString(), currentSerie.getId());
		
		startService(intent);
	}

	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.serie_detail, menu);
		return true;
	}
	
	@Override
	public void update(Observable observable, Object results) {
		seasonAdapter.clear();
		
		final Bundle bundle = (Bundle) results;
		
		
		
		if(bundle.getBoolean(MySeriesConstant.SUCCESS.toString())){
		
			final Bundle data = bundle.getBundle(MySeriesConstant.DATA.toString());
			
			for (String key : data.keySet()) {
				final Season s = (Season) data.getSerializable(key);
				//searchResultsAdapter.add(s);
				System.out.println("Episodes: " + s.getEpisodes().size());
				
				seasonAdapter.addSeason(s);
				
			}
			
		}else{
			String msg = "Search failed! " + bundle.getString(MySeriesConstant.ERROR.toString());
			Toast.makeText(getBaseContext(), msg, Toast.LENGTH_LONG).show();
			
		}
		
	}

}
